package com.atguigu.gmall.web.all.controller;

import com.atguigu.gmall.feign.cart.CartFeignClient;

import com.atguigu.gmall.model.cart.CartInfo;
import com.atguigu.gmall.model.product.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author dyqstart
 * @create 2021-08-17-12:51
 * @since 1.0.0
 */
@Controller
public class CartController {


    @Autowired
    CartFeignClient cartFeignClient;


    /**
     * 远程调用购物车功能，将此商品添加到购物车
     * @param skuId
     * @param skuNum
     * @param
     * @return
     */
    @GetMapping("/addCart.html")
    public String addToCart(@RequestParam("skuId") Long skuId,
                             @RequestParam("skuNum") Integer skuNum,
                             Model model){
        //远程调用购物车功能，将此商品添加到购物车

        //返回skuInfo即可，skuDefaultImg,skuName,id
//        SkuInfo skuInfo = new SkuInfo();
//        skuInfo.setSkuName("华为P40");
//        skuInfo.setSkuDefaultImg("http://39.99.159.121:9000/gmall/16210460331111.png");
//        skuInfo.setId(51L);
        CartInfo cartInfo =  cartFeignClient.addToCart(skuId, skuNum);

        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(cartInfo.getSkuId());
        skuInfo.setSkuName(cartInfo.getSkuName());
        skuInfo.setSkuDefaultImg(cartInfo.getImgUrl());

        model.addAttribute("skuInfo",skuInfo);
        model.addAttribute("skuNum",skuNum);

        return "cart/addCart";
    }

    @GetMapping("/cart.html")
    public String cartListHtml(){

//        List<CartInfo> cartInfos = cartFeignClient.listCart();

        return "cart/index";
    }
}
